package com.mall.shopping.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.shopping.IProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.converter.ProductConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.ItemDesc;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContent;
import com.mall.shopping.dal.persistence.ItemDescMapper;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

@Slf4j
@Service
public class IProductServiceImpl implements IProductService {

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    ItemDescMapper itemDescMapper;

    @Autowired
    ProductConverter productConverter;

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    ContentConverter contentConverter;

    @Autowired
    PanelContentMapper panelContentMapper;


    /**
     * 查询商品详情
     */
    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {
        Item item = itemMapper.selectByPrimaryKey(request.getId());
        if (item == null) {
            ProductDetailResponse response = new ProductDetailResponse();
            response.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
            response.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
            return response;
        }
        ProductDetailDto productDetailDto = productConverter.item2DetailDto(item);
        productDetailDto.setProductImageSmall(Arrays.asList(item.getImages()));
        productDetailDto.setProductImageBig(item.getImages()[0]);

        ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(request.getId());
        productDetailDto.setDetail(itemDesc.getItemDesc());

        ProductDetailResponse response = new ProductDetailResponse();
        response.setProductDetailDto(productDetailDto);
        response.setCode(ShoppingRetCode.SUCCESS.getCode());
        response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        return response;
    }

    /**
     * 商品的分页查询
     * @param request
     * @return
     */
    @Override
    public AllProductResponse getAllProduct(AllProductRequest request) {
        PageHelper.startPage(request.getPage(), request.getSize());
        Example example = new Example(Item.class);
        Example.Criteria criteria = example.createCriteria();
        if (request.getCid() != null && !"".equals(request.getCid())) {
            criteria.andEqualTo("cid",request.getCid());
        }
        if (request.getPriceGt() != null ) {
            criteria.andGreaterThan("price", request.getPriceGt());
        }
        if (request.getPriceLte() != null) {
            criteria.andLessThan("price", request.getPriceLte());
        }
        if ("1".equals(request.getSort())){
            String sort = "price asc";
            PageHelper.orderBy(sort);
        }
        if ("-1".equals(request.getSort())) {
            String sort = "price desc";
            PageHelper.orderBy(sort);
        }
        List<Item> items = itemMapper.selectByExample(example);
        List<ProductDto> productDtos = productConverter.items2Dto(items);
        PageInfo<ProductDto> productDtoPageInfo = new PageInfo<>(productDtos);
        AllProductResponse allProductResponse = new AllProductResponse();
        long total = productDtoPageInfo.getTotal();
        allProductResponse.setTotal(total);
        allProductResponse.setProductDtoList(productDtos);
        return allProductResponse;
    }

    /**
     * 查询推荐商品
     * @return
     */
    @Override
    public RecommendResponse getRecommendGoods() {
        RecommendResponse response = new RecommendResponse();
        Integer position = 1;
        Example example = new Example(Panel.class);
        example.createCriteria().andEqualTo("position",position);
        List<Panel> panels = null;
        try {
            panels = panelMapper.selectByExample(example);
        } catch (Exception e) {
            e.printStackTrace();
            response.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
            response.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
            return response;
        }
        HashSet<PanelDto> panelSet = new HashSet<>();
        for (Panel panel : panels) {
            PanelDto panelDto = contentConverter.panen2Dto(panel);
            Example panelContextExample = new Example(PanelContent.class);
            panelContextExample.createCriteria().andEqualTo("panelId", panel.getId());
            List<PanelContentItemDto> panelContentItemDtos = null;
            try {
                List<PanelContent> panelContents = panelContentMapper.selectByExample(panelContextExample);
                panelContentItemDtos = getPanelContentItemDtoList(panelContents);
            } catch (Exception e) {
                response.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
                response.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
                return response;
            }
            panelDto.setPanelContentItems(panelContentItemDtos);
            panelSet.add(panelDto);
        }
        response.setCode(ShoppingRetCode.SUCCESS.getCode());
        response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        response.setPanelContentItemDtos(panelSet);
        return response;
    }

    /**
     * 根据List<PanelContent> 获得List<PanelContentItemDto>
     * @param panelContents
     * @return
     */
    private List<PanelContentItemDto> getPanelContentItemDtoList(List<PanelContent> panelContents) {
        List<PanelContentItemDto> panelContentItemDtos = contentConverter.panelContent2PaneContentItemDto(panelContents);
        for (PanelContentItemDto panelContentItemDto : panelContentItemDtos) {
            Item item = itemMapper.selectByPrimaryKey(panelContentItemDto.getProductId());
            panelContentItemDto.setSalePrice(item.getPrice());
            panelContentItemDto.setProductName(item.getTitle());
            panelContentItemDto.setSubTitle(item.getSellPoint());
        }
        return panelContentItemDtos;
    }
}
